package com.qf.petmall.starter.hometopic.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.petmall.starter.hometopic.entity.Hometopic;
import com.qf.petmall.starter.hometopic.service.IHometopicService;
import com.qf.petmall.starter.utils.R;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商城页面的商品列表点进去的商品详情页面模块 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-12-06
 */
@CrossOrigin
@RestController
@RequestMapping("/hometopic")
public class HometopicController {

    @Autowired
    IHometopicService iHometopicService;

    /**
     * 查询
     * @return
     */
    @GetMapping("/gethometopic")
    public R get(Integer id){
      Hometopic  hometopic = iHometopicService.listById(id);

        return R.ok().put("hometopic",hometopic);
    }
    /**
     * 添加数据
     * @param hometopic
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Hometopic hometopic){
        iHometopicService.saveone(hometopic);
        return R.ok().put("code","200");
    }

    /**
     *删除
     * @param id
     * @return
     */
    @GetMapping("/del/{id}")
    public R del(@PathVariable("id") Integer id){

        boolean b = iHometopicService.move(id);

        return R.ok().put("code","200");
    }

    /**
     * 修改
     * @param hometopic
     * @return
     */
    @PostMapping("/change")
    public R change(@RequestBody Hometopic hometopic){

/*        UpdateWrapper<Hometopic> hometopicUpdateWrapper = new UpdateWrapper<Hometopic>();

        hometopicUpdateWrapper.setEntity(hometopic);

        boolean update = iHometopicService.update(hometopicUpdateWrapper);*/
        iHometopicService.updateById(hometopic);

        return R.ok().put("code","200");
    }

    /**
     * 收藏
     * @param id
     * @param flag
     * @return
     */
    @GetMapping("/collection")
    public R collection( Integer id, boolean flag){
        if (id==null) {
            return R.error("id不能为空");

        }

        Hometopic hometopic = iHometopicService.getById(id);

        Hometopic hometopic1 = hometopic.setCollection(flag);

        iHometopicService.updateById(hometopic1);
        return R.ok().put("code",200);
    }

   /* @PostMapping("/commont/{id}")
    public R commont(@PathVariable("id") Integer id,@RequestBody String msg){

        Hometopic hometopic = iHometopicService.getById(id);

        Hometopic hometopic1 = hometopic.setComment(msg);

        iHometopicService.updateById(hometopic1);

        return R.ok().put("code",200);
    }*/

}
